package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 工作台今日数据查询
     * @return
     */
    public BusinessDataVO businessData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        Integer totalOrderCount = orderMapper.sumOrderByMap(map); // 订单总数量
        map.put("status", OrderVO.COMPLETED);
        Integer validOrderCount = orderMapper.sumOrderByMap(map); // 有效订单数
        Double orderCompletionRate;
        if(totalOrderCount != 0) {
            orderCompletionRate = (double) (validOrderCount / totalOrderCount); // 订单完成率
        } else {
            orderCompletionRate = 0.0;
        }
        Double totalOrderAmount = orderMapper.sumByMap(map); // 订单总金额
        totalOrderAmount = totalOrderAmount == null ? 0.0 : totalOrderAmount;
        Integer totalUserCount = userMapper.sumByMap(map); // 新增用户总数量
        // 除数不能为0
        Double unitPrice;
        if(totalUserCount != 0) {
            unitPrice = totalOrderAmount / totalUserCount; // 平均客单价
        } else {
            unitPrice = 0.0;
        }
        businessDataVO.setValidOrderCount(validOrderCount); // 有效订单数
        businessDataVO.setOrderCompletionRate(orderCompletionRate); // 订单完成率
        businessDataVO.setTurnover(totalOrderAmount); // 订单总金额
        businessDataVO.setNewUsers(totalUserCount); // 新增用户总数量
        businessDataVO.setUnitPrice(unitPrice); // 平均客单价
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        LocalDate today = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(today, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(today, LocalTime.MAX);
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        Integer allOrders = orderMapper.sumOrderByMap(map); // 全部订单
        map.put("status", OrderVO.COMPLETED);
        Integer completedOrders = orderMapper.sumOrderByMap(map); // 已完成订单
        map.replace("status",OrderVO.CANCELLED);
        Integer cancelledOrders = orderMapper.sumOrderByMap(map); // 已取消订单
        map.replace("status",OrderVO.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = orderMapper.sumOrderByMap(map); // 已派送订单
        map.replace("status",OrderVO.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.sumOrderByMap(map); // 待接单订单


        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(allOrders); // 全部订单
        orderOverViewVO.setCompletedOrders(completedOrders); // 已完成订单
        orderOverViewVO.setCancelledOrders(cancelledOrders); // 已取消订单
        orderOverViewVO.setDeliveredOrders(deliveredOrders); // 已派送订单
        orderOverViewVO.setWaitingOrders(waitingOrders); // 待接单订单

        return orderOverViewVO;
    }

    /**
     * 查询菜品总览
     * @return
     */
    public DishOverViewVO getDishOverView() {
        // 根据状态获取菜品的数量
        Integer enabledDish = dishMapper.countByStatus(StatusConstant.ENABLE);
        Integer disabledDish = dishMapper.countByStatus(StatusConstant.DISABLE);
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(enabledDish); // 已启用菜品数量
        dishOverViewVO.setDiscontinued(disabledDish); // 已禁用菜品数量
        return dishOverViewVO;
    }

    /**
     * 查询套餐总览
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {
        // 根据状态获取套餐的数量
        Integer enabledSetmeal = setmealMapper.countByStatus(StatusConstant.ENABLE);
        Integer disabledSetmeal = setmealMapper.countByStatus(StatusConstant.DISABLE);
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(enabledSetmeal); // 已启用套餐数量
        setmealOverViewVO.setDiscontinued(disabledSetmeal); // 已禁用套餐数量
        return setmealOverViewVO;
    }

}
